package com.skywolf.chem.live.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.customer.entity.Customer;
import com.skywolf.chem.customer.service.ICustomerService;
import com.skywolf.chem.live.entity.Videocomment;
import com.skywolf.chem.live.entity.Videoscore;
import com.skywolf.chem.live.mapper.VideocommentMapper;
import com.skywolf.chem.live.param.CommentDto;
import com.skywolf.chem.live.param.CommentParam;
import com.skywolf.chem.live.param.CommentQueryDto;
import com.skywolf.chem.live.param.VideoParam;
import com.skywolf.chem.live.service.IVideocommentService;
import com.skywolf.chem.live.service.IVideoscoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 视频评论表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-12-01
 */
@Service
public class VideocommentServiceImpl extends ServiceImpl<VideocommentMapper, Videocomment> implements IVideocommentService {

    @Autowired
    VideocommentMapper commentMapper;

    @Autowired
    ICustomerService customerService;

    @Autowired
    IVideoscoreService scoreService;

    @Override
    public PageVO<CommentDto> pageCommentList(CommentQueryDto queryParam,Long uid) {
        Long startNum = queryParam.getPageSize()*(queryParam.getPageNum()-1);
        Long endNum = queryParam.getPageSize();
        List<CommentDto> list = commentMapper.getCommentPageList(startNum,endNum,queryParam.getId(),uid);
        Integer count = commentMapper.getCommentPageCount(queryParam.getId(),uid);
        PageVO<CommentDto> pageVO = new PageVO(list,Long.valueOf(count),queryParam.getPageNum(),queryParam.getPageSize());
        return pageVO;
    }

    @Override
    public void addComment(VideoParam param, Long uid) {
        Customer customer = customerService.getById(uid);
        Videocomment videocomment = new Videocomment();
        videocomment.setUserId(uid);
        videocomment.setUserName(customer.getUserName());
        videocomment.setContent(param.getContent());
        videocomment.setVideoId(param.getVideoId());
        if(param.getCommentId()!=null && param.getCommentId()!=0){
            Videocomment comment = this.getById(param.getCommentId());
            if(comment!=null){
                videocomment.setReplyId(param.getCommentId());
                videocomment.setReplyUid(comment.getUserId());
                videocomment.setReplyName(comment.getUserName());
            }
        }
        this.save(videocomment);
    }

    @Override
    public void scoring(CommentParam param, Long uid) {
        UpdateWrapper<Videoscore> wrapper = new UpdateWrapper<>();
        wrapper.eq("comment_id", param.getCommentId()).eq("user_id",uid);
        Videoscore videoscore = scoreService.getOne(wrapper);
        if(videoscore==null){
            videoscore = new Videoscore();
            videoscore.setUserId(uid);
            videoscore.setCommentId(param.getCommentId());
        }
        videoscore.setScore(param.getContent());
        scoreService.saveOrUpdate(videoscore);
    }

    @Override
    public void delComment(Long uid, Long commentId)throws ServiceException {
        UpdateWrapper<Videocomment> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", commentId).eq("user_id",uid);
        Videocomment comment = this.getOne(wrapper);
        if(comment==null){
            throw new ServiceException("只能删除自己的评论！");
        }
        commentMapper.deleteById(commentId);
    }
}
